package com.tiancheng.onecode.task;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author jiangjun
 * @description 定时任务执行
 * @date 2024/11/29 17:28
 */
@FunctionalInterface
public interface MyScheduledTask {

    Logger logger = LoggerFactory.getLogger(MyScheduledTask.class);

    void execute();

    // 默认方法：日志记录逻辑
    default void process(Runnable runnable) {
        String taskName = this.getClass().getSimpleName();
        long startTime = System.currentTimeMillis();

        try {
            logger.info("Starting task: {}", taskName);
            runnable.run();
            // 获取任务完成的时间
            long endTime = System.currentTimeMillis();
            logger.info("Completed task: {}, execute time: {} ms", taskName, (endTime - startTime));
        } catch (Exception e) {
            logger.error("Error occurred while executing task: " + taskName);
            e.printStackTrace();
        }
    }
}
